// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MemIO.java

package mechanics;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class MemIO {

	public String lastReading = "0";
	
	public MemIO(String ip) {
		socket = null;
		String locIp;
		int locPort;
		if (ip.indexOf(":", 7) != -1) {
			locIp = ip.substring(0, ip.indexOf(":", 7));
			locPort = Integer.parseInt(ip.substring(ip.indexOf(":", 7) + 1, ip
					.length()));
		} else {
			locIp = ip;
			locPort = 8116;
		}
		try {
			socket = new Socket(locIp, locPort);
		} catch (UnknownHostException e) {
			System.out.println("no such host");
		} catch (IOException io) {
			System.out.println(io.toString());
		}
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			System.out.println(in.readLine());
		} catch (IOException io) {
			System.out.println(io.toString());
		}
	}

	public void write(int a, int b) {
		if (a < 768) {
			sendCmd("w " + a + " " + b);
		} else {
			System.out.println("Illegal address");
			System.exit(0);
		}
	}

	public short read(int a) {
		short r = -1;
		if (a < 768)
			r = (short) Integer.parseInt(sendCmd("r " + a));
		else
			switch (a) {
			case 768:
				r = (short) Integer.parseInt(sendCmd("t 1"));
				break;

			case 769:
				r = (short) Integer.parseInt(sendCmd("t 2"));
				break;

			case 770:
				r = (short) Integer.parseInt(sendCmd("t 3"));
				break;

			case 771:
				r = (short) Integer.parseInt(sendCmd("t 4"));
				break;

			case 772:
				r = (short) Integer.parseInt(sendCmd("t 5"));
				break;

			case 784:
				r = (short) Integer.parseInt(sendCmd("h 1"));
				break;

			case 785:
				r = (short) Integer.parseInt(sendCmd("h 2"));
				break;

			case 786:
				r = (short) Integer.parseInt(sendCmd("h 3"));
				break;

			case 787:
				r = (short) Integer.parseInt(sendCmd("h 4"));
				break;

			case 788:
				r = (short) Integer.parseInt(sendCmd("h 5"));
				break;

			case 773:
			case 774:
			case 775:
			case 776:
			case 777:
			case 778:
			case 779:
			case 780:
			case 781:
			case 782:
			case 783:
			default:
				System.out.println("Illegal address");
				System.exit(0);
				break;
			}
		return r;
	}

	private String sendCmd(String c) {
		String response = "";
		if (socket != null) {
			try {
				PrintWriter out = new PrintWriter(socket.getOutputStream(),
						true);
				out.println(c);
			} catch (IOException io) {
				System.out.println(io.toString());
			}
			try {
				BufferedReader input = new BufferedReader(
						new InputStreamReader(socket.getInputStream()));
				response = input.readLine();
			} catch (IOException io) {
				System.out.println(io.toString());
			}
		}
		if(response.equals(" ") || Integer.valueOf(response) < 0) {
			return this.lastReading;
		}
		
		this.lastReading = response;
		
		return response;
	}

	public void finalize() {
		try {
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			out.println("exit");
			socket.close();
		} catch (IOException io) {
			System.out.println(io.toString());
		}
		System.exit(0);
	}

	private Socket socket;
}
